package jp.co.withone.osgi.gadget.bundlecontroller.manager;

import java.awt.TrayIcon.MessageType;
import java.net.URL;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;

import jp.co.withone.osgi.gadget.bundlecontroller.balloon.BalloonViewService;
import jp.co.withone.osgi.gadget.bundlecontroller.viewer.SettingViewListener;
import jp.co.withone.osgi.gadget.bundlecontroller.viewer.impl.BundleActionCommand;
import jp.co.withone.osgi.gadget.bundlecontroller.viewer.impl.InstallView;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

public class ViewManager {

    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle("jp.co.withone.osgi.gadget.bundlecontroller.resource.MessageResource");
    
    private static final Object lockObject = new Object();
    private static final String KEY_MESSAGE_ALLBUNDLES ="balloon.message.allbundles";
    private static final String KEY_MESSAGE_BUNDLEID="balloon.message.bundleid";
    private static final String KEY_MESSAGE_UNKNOW_CONTROL="balloon.message.unknown";


    private ServiceTracker  settingViewServiceTracker = null;
    private ServiceTracker  balloonServiceTracker = null;
    
    private InstallView installView = null;
    
    
    private BundleContext context = null;
    
    public ViewManager(BundleContext context){
        this.context = context;
    }
    
    public BundleContext getBundleContext(){
        return context;
    }
    
    public void init(){
        settingViewServiceTracker = new ServiceTracker(context, SettingViewListener.class.getName(), null);
        settingViewServiceTracker.open();
        
        balloonServiceTracker = new ServiceTracker(context, BalloonViewService.class.getName(), null);
        balloonServiceTracker.open();
    }
    
    public void end(){
        if (settingViewServiceTracker != null){
            settingViewServiceTracker.close();
            settingViewServiceTracker = null;
        }
        
        if (balloonServiceTracker != null){
            balloonServiceTracker.close();
            balloonServiceTracker = null;
        }
    }
    
    public ServiceReference[] getViewServiceReferences(){
        return settingViewServiceTracker.getServiceReferences();
    }
    
    public SettingViewListener getSettingViewListener(Bundle bundle){
        ServiceReference[] refs = settingViewServiceTracker.getServiceReferences();
        if (refs == null) {
            return null;
        }
        for (ServiceReference serviceReference : refs) {
            if (serviceReference.getBundle().getBundleId() == bundle.getBundleId()){
                return (SettingViewListener)settingViewServiceTracker.getService(serviceReference);
                
            }
        }
        return null;
    }
    
    public BalloonViewService getBalloonService(){
        return (BalloonViewService)balloonServiceTracker.getService();
    }
    
    public InstallView getInstallView(){
        synchronized (lockObject) {
            if (installView == null) {
                installView = new InstallView(this);
            }
        }
        return installView;
        
    }
    
    public String getBundleDispName(Bundle bundle){
        if (bundle == null) {
            return "";
        }
        String dispName = (String)bundle.getHeaders().get(Constants.BUNDLE_NAME);
        if (dispName == null) {
            dispName = bundle.getLocation();
        }
        return dispName;
    }

    public ImageIcon getImageIcon(BundleActionCommand actionCommand){
        if (actionCommand.getIconLocation() == null) {
            return null;
        }
        return getImageIcon(actionCommand.getIconLocation());
    }

    public ImageIcon getImageIcon(String path){
        URL url = getBundleContext().getBundle().getResource(path);
        ImageIcon icon = null;
        if (url != null) {
            icon = new ImageIcon(url);
        }
        return icon;
    }
    
    public String getMessage(String key){
        return RESOURCE_BUNDLE.getString(key);
    }

    public void displayBundleControledMessage(BundleActionCommand actionCommand,Bundle bundle){
        BalloonViewService balloonViewService =getBalloonService();
        if (balloonViewService == null) {
            return ;
        }
        balloonViewService.displayMessage(MessageType.INFO, getCaption(actionCommand,bundle) , getBundleDispName(bundle));
    }
    
    private String getCaption(BundleActionCommand actionCommand ,Bundle bundle){
        String caption = getMessage(actionCommand.getBalloonLabel());
        if (caption == null) {
            return getMessage(KEY_MESSAGE_UNKNOW_CONTROL);
        }
        StringBuilder stringBuilder = new StringBuilder(caption);
        if (bundle != null) {
            stringBuilder.append(" ");
            stringBuilder.append(getMessage(KEY_MESSAGE_BUNDLEID));
            stringBuilder.append(bundle.getBundleId());
        } else {
            stringBuilder.append(getMessage(KEY_MESSAGE_ALLBUNDLES));
        }
        return stringBuilder.toString();
    }

}
